﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Web_User.Models;

namespace Web_User.Controllers
{
    public class DefaultController : Controller
    {
        private readonly IUseService _service;
        public DefaultController(IUseService service)
        {
            _service = service;
        }
        public ActionResult Index()
        {
            return View(_service.Search());
        }

        public ActionResult Details(Guid id)
        {
            return View(_service.Details(id));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(UserInfo info)
        {
            try
            {
                info.Id = Guid.NewGuid();
                _service.Add(info);

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Edit(Guid id)
        {
            return View(_service.Details(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UserInfo info)
        {
            try
            {
                _service.Edit(info);
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(Guid id)
        {
            return View(_service.Details(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(UserInfo info)
        {
            try
            {
                _service.Del(info);
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}